//
//  QDLoginController.m
//  simulateQuickDoctor
//
//  Created by 姚华杰 on 2017/3/23.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDLoginController.h"
#import "QDNetworkTool.h"
#import "QDLoginModel.h"
#import "QDUserDefaultTool.h"
#import "QDBannerTools.h"

@interface QDLoginController ()

@property (weak, nonatomic) IBOutlet UITextField *txt_phone;
@property (weak, nonatomic) IBOutlet UITextField *txt_password;

@end

@implementation QDLoginController{
    NSUInteger type;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"QDLoginController" bundle:nil];
//    [sb instantiateInitialViewController];
//    [sb instantiateViewControllerWithIdentifier:@"ID"];

    type = [self.parameters[@"type"] integerValue];
    
    NSLog(@"%zd",type);
    
    if(type == 0)
    {
        self.navigationItem.title = @"注册";
    }
    else
    {
        self.navigationItem.title = @"登录";
    }

    
}

- (void)setupUI
{
 }


//点击短信验证按钮调用
- (IBAction)SMSVerification:(UIButton *)sender {
}

//点击登录按钮调用
- (IBAction)login:(UIButton *)sender {
    
    if(!([_txt_phone.text isEqualToString:@"110"] && [_txt_password.text isEqualToString:@"pipixia"])){
        [QDBannerTools pp_BannerErrorTitle:@"手机或密码错误" andTime:1.0];
        return;
    }
    
    [QDBannerTools pp_BannerTitle:@"登录中.." andTime:1.5];
    
    NSString *url = @"http://iosapi.itcast.cn/doctor/carelinkQuickLogin.json.php";
    NSDictionary *parameters = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
    QDNetworkTool* tool =[QDNetworkTool sharedTool];
    tool.requestSerializer = [AFJSONRequestSerializer serializer];
    [tool requestWithHTTPMethod:@"POST" URLString:url parameters:parameters CallBack:^(id responseObj) {
        
        NSLog(@"%@",responseObj);
        if(responseObj != nil){
            
            if([responseObj[@"msg"] isEqualToString:@"OK"]){
            
                NSDictionary* modelDict = responseObj[@"data"];
                
                QDLoginModel* model = [QDLoginModel new];
                
                [model setValuesForKeysWithDictionary:modelDict];
            
                NSData* userInfoData = [NSKeyedArchiver archivedDataWithRootObject:model];
                
                [QDUserDefaultTool saveObject:userInfoData forKey:@"userinfo"];
                
                NSLog(@"%@",NSHomeDirectory());
                
                
                void(^loginBlock)() = self.parameters[@"loginBlock"];
                
                loginBlock();
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }
        }
        
    }];
}

//点击注册新账号调用
- (IBAction)setRegister:(UIButton *)sender {
}

//点击忘记密码调用
- (IBAction)retrievePassword:(UIButton *)sender {
}
@end
